package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCategoryId(Map<String, Object> params, long categoryId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        // 当参数为0 表示 查询所有数据
        if (categoryId == 0) {
            if (StringUtils.isNotEmpty(key)) {
                wrapper.eq("attr_group_id", key).or().like("attr_group_name", key);
            }

            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", categoryId);
            //进行多字段 匹配
            if (StringUtils.isNotEmpty(key)) {
                wrapper.eq("attr_group_id", key).or().like("attr_group_name", key);
            }
            IPage<AttrGroupEntity> entityIPage = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(entityIPage);
        }
    }

    /**
     * 获取同一分类下  所有的属性分组 与 属性
     * @param catId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAllAttrWithgroupMessage(long catId) {
        List<AttrGroupEntity> catelog_id = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId));
        List<AttrGroupWithAttrsVo> collect = catelog_id.stream().map((attrGroup) -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroup, attrsVo);
            List<AttrEntity> allRelations = attrService.getAllRelations(attrGroup.getAttrGroupId());
            attrsVo.setAttrs(allRelations);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

}